package api

import (
	"buding-ai/internal/service"
	"buding-ai/internal/web/dto"
	"buding-ai/internal/web/response"
	"buding-ai/pkg/utils/paginate"
	"github.com/gin-gonic/gin"
)

type AgentRoleApi struct {
	router *gin.RouterGroup
	srv    *service.AgentRoleService
	BaseApi
}

func NewAgentRoleApi(router *gin.RouterGroup) *AgentRoleApi {
	api := &AgentRoleApi{
		router: router,
		srv:    service.NewAgentRoleService(),
	}
	api.router.POST("", api.Add)
	api.router.GET("/:id", api.Info)
	api.router.DELETE("/:id", api.Delete)
	api.router.PUT("", api.Update)
	api.router.GET("/page", api.Page)
	return api
}

func (api *AgentRoleApi) Add(ctx *gin.Context) {
	var agentDTO dto.AgentRoleDTO
	if err := ctx.ShouldBindJSON(&agentDTO); err != nil {
		response.ParamBindError(ctx, err)
		return
	}
	if err := api.srv.Create(agentDTO); err != nil {
		response.InternalError(ctx, "创建角色失败", err)
		return
	}
	response.SuccessMsg(ctx, "添加角色成功")
}

func (api *AgentRoleApi) Info(ctx *gin.Context) {
	var id uint
	var err error
	id, err = api.GetParamUint(ctx, "id")
	if err != nil {
		response.ParamBindError(ctx, err)
		return
	}
	var resp *response.AgentRoleInfoVO
	resp, err = api.srv.GetById(id)
	if err != nil {
		response.InternalError(ctx, "查询失败", err)
		return
	}
	response.SuccessWithData(ctx, resp)
}
func (api *AgentRoleApi) Delete(ctx *gin.Context) {
	var id uint
	var err error
	id, err = api.GetParamUint(ctx, "id")
	if err != nil {
		response.ParamBindError(ctx, err)
		return
	}
	if err := api.srv.DeleteById(id); err != nil {
		response.InternalError(ctx, "删除失败：", err)
		return
	}
	response.SuccessMsg(ctx, "删除成功")

}
func (api *AgentRoleApi) Update(ctx *gin.Context) {
	var agentDTO dto.AgentRoleDTO
	if err := ctx.ShouldBindJSON(&agentDTO); err != nil {
		response.ParamBindError(ctx, err)
		return
	}
	if err := api.srv.Update(agentDTO); err != nil {
		response.InternalError(ctx, "修改角色失败", err)
		return
	}
	response.SuccessMsg(ctx, "修改角色成功")
}
func (api *AgentRoleApi) Page(ctx *gin.Context) {
	// 获取分页参数
	pageRequest := paginate.GetPageParams(ctx)

	pageResult, err := api.srv.GetPage(pageRequest)
	if err != nil {
		response.InternalError(ctx, "获取角色列表失败", err)
		return
	}

	response.SuccessWithData(ctx, pageResult)
}
